<template>
  <div class="tableForm">
    <a-form
      :model="pageInfo"
      name="horizontal_login"
      layout="inline"
      autocomplete="off"
    >
      <a-form-item label="考试名称">
        <a-input v-model:value="pageInfo.examTitle" placeholder="考试名称" />
      </a-form-item>
      <a-form-item label="是否通过">
        <JDictSelectTag v-model:value="pageInfo.passed" dictCode="exam_result"
                        placeholder="是否通过" style="width: 200px;" />
      </a-form-item>
      <a-form-item>
        <a-space size="large">
          <a-button type="primary" @click="onSearch" :icon="h(SearchOutlined)">查询</a-button>
          <a-button @click="clearSearch" :icon="h(SyncOutlined)">清除</a-button>
        </a-space>
      </a-form-item>
    </a-form>
    <a-spin :spinning="loading">
      <template v-if="userExamItems?.length > 0">
        <a-table
          :columns="columns"
          :data-source="userExamItems"
          :loading="loading"
          :pagination="paginationTable"
          @change="handleTableChange"
          bordered
          style="margin-top: 10px"
        >
          <template #bodyCell="{ record,column }">
            <template v-if="column.key === 'action'">
              <a-button type="primary" size="small"
                        @click="router.push(`/userCenter/exam/examScore/userExamResult/${record.examId}`)">
                查看详情
              </a-button>
            </template>
            <template v-else-if="column.key === 'accuracy'">
              {{ record.accuracy || 0 }}%
            </template>
            <template v-else-if="column.key === 'passed'">
              <a-tag v-if="record.passed === 1" color="#87d068">通过</a-tag>
              <a-tag v-else-if="record.passed === 0" color="#f50">未通过</a-tag>
            </template>
          </template>
        </a-table>
      </template>
      <EmptyData v-else />
    </a-spin>
  </div>
</template>
<script lang="ts" setup>
import { h, onMounted, reactive, ref } from "vue";
import { apiUserExamResultList } from "@/api/exam";
import { useRouter } from "vue-router";
import { SearchOutlined, SyncOutlined } from "@ant-design/icons-vue";
import JDictSelectTag from "@/components/public/JDictSelectTag.vue";
import EmptyData from "@/components/public/EmptyData.vue";

const router = useRouter();

const userExamItems = ref<any>([]);
const total = ref<number>(0);
const loading = ref<boolean>(false);

const paginationTable = reactive({
  total: 0,
  current: 1,
  pageSize: 10,//每页中显示10条数据
  showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
});

const columns = [
  {
    title: "考试名称",
    dataIndex: "examTitle",
    width: "25%"
  },
  {
    title: "考试次数",
    dataIndex: "tryCount",
    width: "15%"
  },
  {
    title: "最高分数",
    dataIndex: "maxScore",
    width: "15%"
  },
  {
    title: "是否通过",
    dataIndex: "passed",
    key: "passed",
    width: "15%"
  },
  {
    title: "最近考试",
    dataIndex: "updateTime",
    width: "20%"
  },
  {
    title: "操作",
    key: "action",
    dataIndex: "action"
  }
];

async function handleTableChange(pagination) {
  paginationTable.current = pagination.current;
  paginationTable.pageSize = pagination.pageSize;
  pageInfo.value.pageNo = pagination.current;
  pageInfo.value.pageSize = pagination.pageSize;
  await getUserExamList();
}

const pageInfo = ref<{
  pageNo: number;
  pageSize: number;
  examTitle: string;
  passed: string;
}>({
  // 当前页码
  pageNo: 1,
  // 当前每页显示多少条数据
  pageSize: 10,
  examTitle: "",
  passed: ""
});

async function getUserExamList() {
  loading.value = true;
  userExamItems.value = [];
  const data: any = await apiUserExamResultList(pageInfo.value);
  userExamItems.value = data.result?.records;
  total.value = data.result?.total;
  loading.value = false;
}

function onSearch() {
  pageInfo.value.pageNo = 1;
  getUserExamList();
}

function clearSearch() {
  pageInfo.value.examTitle = "";
  pageInfo.value.passed = "";
  onSearch();
}

onMounted(() => {
  getUserExamList();
});

</script>
<style lang="less" scoped>
.tableForm {
  padding: 0 10px;
  height: 87.6vh;
}
</style>
